package adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.media.MediaMuxer;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.wyy.R;

import java.util.List;

import entiy.MusicInfo;

public class ListViewAdapter extends BaseAdapter {
    private List<MusicInfo> list ;
    private Context context;

    public ListViewAdapter(Context context, List<MusicInfo>infos){
        this.context = context;
        list = infos;
    }
    @Override
    public int getCount() {
        //Log.e("TAG", "getCount: "+list.size() );
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = new Holder();

        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item,null);
            holder.bitmap = convertView.findViewById(R.id.bitmap_music);
            holder.tv_art = convertView.findViewById(R.id.art);
            holder.tv_title = convertView.findViewById(R.id.title);

            convertView.setTag(holder);
        }else {
            holder = (Holder) convertView.getTag();
        }
        MusicInfo musicInfo = list.get(position);
        holder.tv_title.setText(musicInfo.getTitle());
        holder.tv_art.setText(musicInfo.getArtlist());
        holder.bitmap.setImageBitmap(musicInfo.getBitmap());

        return convertView;
    }

    class Holder {
        public ImageView bitmap;
        public TextView tv_title;
        public TextView tv_art;
    }
}
